/*
 *  Copyright 2015-2016, RenGu Technology (BeiJing) Co., Ltd.
 *  All right reserved.
 *
 *  THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF RENGU TECHNOLOGY (BeiJing) CO., LTD.
 *
 *  THE CONTENTS OF THIS FILE MAY NOT BE DISCLOSED TO THIR PARTIES,
 *  COPIED OR DUPLICATED IN ANY FORM, IN WHOLE OR IN PART,
 *  WITHOUT THE PRIOR WRITTEN PERMISSION OF RENGU TECHNOLOGY (BeiJing) CO., LTD.
 *
 *  IntelliCalService.java
 *
 *  Edit History:
 *      lizhangtao Created on 2022年01月11日
 *
 */
package cc.rengu.igas.route.core.service.trans;

import cc.rengu.igas.route.common.enums.RespCodeEnum;
import cc.rengu.igas.route.common.enums.SmartRouteRspEnums;
import cc.rengu.igas.route.core.realize.impl.WeightCfgRouterChoice4POCImpl;
import cc.rengu.igas.route.facade.bean.CalChannelInfo;
import cc.rengu.igas.route.facade.request.IntelliCalRequest;
import cc.rengu.igas.route.facade.response.IntelliCalResponse;
import cc.rengu.oltp.service.base.RadpService;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.enums.TransStatusEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.base.StringUtil;

import java.util.List;

/**
 * 路由试算
 *
 * @author lizhangtao
 * @version 1.0
 * @date 2022/1/11
 */
public class IntelliCalService extends RadpService {
    /**
     * Biz init int.
     *
     * @param s the s
     * @return the int
     */
    @Override
    protected int bizInit(String s) {
        return 0;
    }

    /**
     * Biz process int.
     *
     * @param s the s
     * @param i the
     * @return the int
     */
    @Override
    protected int bizProcess(String s, int i) {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        try {
            IntelliCalRequest request = new IntelliCalRequest();
            ConvertUtil.convertOutput(request);
            //参数为空判断
            if (StringUtil.isNullorEmpty(request.getHeader().getInstId()) ||
                    StringUtil.isNullorEmpty(request.getHeader().getSrcSysId()) ||
                    StringUtil.isNullorEmpty(request.getHeader().getChanlId())) {
                rglog.error("必须输入机构号<{}>，发起系统<{}>、渠道号<{}>", request.getHeader().getInstId(),
                        request.getHeader().getSrcSysId(), request.getHeader().getChanlId());
                throw new BizException(SmartRouteRspEnums.MSG_VALID_CHECK_ERROR.getRespCode(), SmartRouteRspEnums.MSG_VALID_CHECK_ERROR.getRespDesc());
            }

            xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.SRC_SYS_ID, request.getHeader().getSrcSysId());
            xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM, request.getHeader().getTraceNo());
            xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TRANS_TIME, request.getHeader().getTransTime());
            xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TRANS_DATE, request.getHeader().getTransDate());

            WeightCfgRouterChoice4POCImpl router = new WeightCfgRouterChoice4POCImpl();
            List<CalChannelInfo> calChannelInfos = router.routerChoice(request);
            IntelliCalResponse response = new IntelliCalResponse();
            response.setHeader(request.getHeader());
            response.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            response.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
            response.setStatus(TransStatusEnum.TRANS_SUCC.getStatus());
            response.setFeeWeight(request.getFeeWeight());
            response.setStressWeight(request.getStressWeight());
            response.setSuccRateWeight(request.getSuccRateWeight());
            response.setTimeCostWeight(request.getTimeCostWeight());
            response.setCalChannelInfoList(calChannelInfos);
            ConvertUtil.convertInput(response);
        } catch (BizException e) {
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, e.getCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, e.getMessage());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e1));
            }
        } catch (Exception e) {
            rglog.error(StringUtil.ExceptionToString(e));
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, SmartRouteRspEnums.TRANS_FAIL.getRespCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, SmartRouteRspEnums.TRANS_FAIL.getRespDesc());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e1));
            }
        }
        return 0;
    }

    /**
     * Biz terminate int.
     *
     * @param s the s
     * @return the int
     */
    @Override
    protected int bizTerminate(String s) {
        return 0;
    }
}
